package com.telit.uav.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.telit.uav.dao.CommUavDao;
import com.telit.uav.dao.UavMediaSourceDao;
import com.telit.uav.dto.UavMediaSourceDTO;
import com.telit.uav.entity.FileUploadLog;
import com.telit.uav.service.UavMediaSourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zhouyu
 * @description 文件上传实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UavMediaSourceServiceImpl implements UavMediaSourceService {

    @Autowired
    private UavMediaSourceDao fileUploadLogDao;

    @Autowired
    private CommUavDao commUavDao;


    @Override
    public void uploadFile(FileUploadLog fileUploadLog) {
        String uavCode = fileUploadLog.getUavCode();
        String uavName = commUavDao.queryNameByCode(uavCode);
        fileUploadLog.setUavName(uavName);
        fileUploadLogDao.insertSelective(fileUploadLog);
    }

    /**
     * 分页条件查询无人机多媒体资源
     *
     * @param current
     * @param size
     * @param uavMediaSourceDTO
     *
     * @return
     */
    @Override
    public IPage<UavMediaSourceDTO> listByPage(Integer current, Integer size, UavMediaSourceDTO uavMediaSourceDTO) {
        IPage<UavMediaSourceDTO> uavMediaSourceIPage = new Page<>(current, size);
        uavMediaSourceIPage = fileUploadLogDao.selectByPage(uavMediaSourceIPage, uavMediaSourceDTO);
        List<UavMediaSourceDTO> records = uavMediaSourceIPage.getRecords();
        List<UavMediaSourceDTO> resultList = new ArrayList<UavMediaSourceDTO>();
        if (!CollectionUtils.isEmpty(records)) {
            records.forEach(record -> {
                if (record != null) {
                    if (StringUtils.isNotBlank(record.getMediaUrl())) {
                        //转换路径为预览路径
                        String filepath = record.getMediaUrl();
                        //TODO:v1.1.0
//                        String mediaUrl = MinioUtils.previewImg(filepath);
//                        record.setMediaUrl(mediaUrl);
                        resultList.add(record);
                    }
                    //TODO南京浦口暂时性操作
                    if (StrUtil.equals(record.getEquipmentModel(), "10018")) {
                        record.setEquipmentModel("经纬M300RTK");
                    }
                }

            });
        }
        uavMediaSourceIPage.setRecords(resultList);
        return uavMediaSourceIPage;
    }

}
